import Core.IO as IO
import Core.MongoDB as MongoDB
import Core.Gadget as Gadget
import datetime
from WindPy import *
w.start()

database = MongoDB.MongoDB("10.13.144.185", "27017")
#stocks = StockList(database)

#database.creatIndex("Factor", "Dividend", "Key")
#database.creatIndex("Factor", "Dividend", "StdDateTime")
#database.creatIndex("Factor", "Dividend", "Symbol")

instruments = database.find("Instruments", "Stock")

# symbols = stocks[0]
# ipo_dates = stocks[1]

# for i in range(len(symbols)):
count = 0
for instrument in instruments:
    count += 1
    if count < 1000:
        continue

    symbol = instrument["Symbol"]
    ipo_date = instrument["DateTime1"]
    delist_date = instrument["DateTime2"]
    print(symbol, count, datetime.now())

    # ipo_date = Gadget.ToUTCDateTime(ipo_date)
    datetime1 = Gadget.ToUTCDateTime(datetime(2005, 1, 1, 8, 0, ))
    datetime2 = Gadget.ToUTCDateTime(datetime.utcnow())
    #
    if ipo_date > datetime1:
        datetime1 = ipo_date
    if delist_date < datetime2:
        datetime2 = delist_date

    # ---返回的都是当天24点值，也就是第二天0点数值---
    month_list = Gadget.GenerateReportDates(datetime1,datetime2)

    for j in range(len(month_list)):
        std_end_datetime = month_list[j]
        if std_end_datetime.year % 4 == 0:
            lastyear_datetime = month_list[j] + timedelta(days=-366)
        else:
            lastyear_datetime = month_list[j] + timedelta(days=-365)

        std_end_datetime = std_end_datetime + timedelta(hours=15)
        lastyear_datetime = lastyear_datetime + timedelta(hours=15)
        factors_PE = database.find("Factor", "PriceEarningNetIncomeTTM", std_end_datetime + timedelta(days=-10), std_end_datetime,query={"Symbol": symbol})
        if len(factors_PE) == 0:
            continue
        factors_PE = factors_PE[-1]
        factors_PE_lastyear = database.find("Factor", "PriceEarningNetIncomeTTM", lastyear_datetime + timedelta(days=-10),lastyear_datetime, query={"Symbol": symbol})
        if len(factors_PE_lastyear) == 0:
            continue
        factors_PE_lastyear = factors_PE_lastyear[-1]

        quotes = database.find("Quote", symbol + "_Time_86400_Bar", lastyear_datetime+ timedelta(days=-1),std_end_datetime )
        if len(quotes) < 2:
            continue
        quote = quotes[-1]
        quote_lastyear = quotes[0]
        cap = quote["Close"] * quote["Values"]['TotalShares']
        cap_lastyear = quote_lastyear["Close"] * quote_lastyear["Values"]['TotalShares']
        debug = 1
        earning = cap/factors_PE["Value"]
        earning_lastyear = cap_lastyear/factors_PE_lastyear["Value"]
        GrowthEarning1Yr = (earning - earning_lastyear)/abs(earning)
        debug = 1
        if earning > 0 and earning_lastyear < 0:
            remark = "P"
        elif earning < 0 and earning_lastyear > 0:
            remark = "L"
        else:
            remark = "N"

        followersDoc = {}
        followersDoc["Symbol"] = symbol
        followersDoc["Value"] = GrowthEarning1Yr
        followersDoc["Remark"] = remark
        followersDoc["StdDateTime"] = Gadget.ToUTCDateTime(std_end_datetime)
        followersDoc["Key"] = symbol + "_Dividend_Factor_" + str(std_end_datetime)+".000"
        #print(symbol + "_Dividend_Factor_" + str(std_end_datetime)+".000", GrowthEarning1Yr, remark)
        #database.upsert("Factor", "GrowthEarning1Y_by_zhu", {"Key": followersDoc["Key"]}, followersDoc)

